import urllib.request
import ssl
import time
import os


def get_ipv6():
    ssl._create_default_https_context = ssl._create_unverified_context
    response = urllib.request.urlopen('http://ipv6.jp2.test-ipv6.com/ip/')
    value_get = response.read().decode('utf-8')
    value = eval(value_get[9:-2])
    if value['type'] != 'ipv6':
        raise Exception('不支持ipv6！', value)
    else:
        return value['ip']


def paper_to_set(ipv6):
    t = str(time.time())
    with open('new.txt', 'w') as f:
        f.write(t)
        f.write('\n')
        f.write(ipv6)


def set_to_gitee():
    commands = ['git add .',
                '''git commit -m "commit by XCP's python program"''',
                'git push -u origin "master"']
    for c in commands:
        os.system(c)


def sleep(t):
    time.sleep(t)


if __name__ == '__main__':
    while 1:
        ipv6 = get_ipv6()
        print(ipv6)
        paper_to_set(ipv6)
        set_to_gitee()
        sleep(60)
